# -*- coding: utf-8 -*-
from ..models.models import SType as ST

from logging import debug
from odoo.exceptions import UserError
from odoo import http
import json

class Student(http.Controller):
    @http.route('/student/student/', auth='public', website=True)
    def index(self, **kw):
        # return "显示学生列表"
        return http.request.render('student.indexing', {
            'title': '学生数据',
            'root': '/student/student',
            # 'objects': http.request.env['student.student'].search([]),
        })




    @http.route('/student/student/objects/', auth='public', website=True)
    def list(self, **kw):
        # model = http.request.env['student.student']
        model = http.request.env[ ST.student ]
        list = model.search([])
        # student.student(1, 18, 21, 23)
        # print('列表----------------', list, type(list))
        return http.request.render('student.listing', {
            'title': '学生列表',
            'root': '/student/student',
            'objects': list,
        })

    @http.route('/student/student/objects/<model("student.student"):obj>/', auth='public', website=True)
    def object(self, obj, **kw):
        # print('访问详情 -----------------------------', kw)
        return http.request.render('student.object', {
            'object': obj,
            'root': '/student/student',
        })




    # 新增一名学生
    @http.route('/student/add/', auth='public', website=True)
    def add(self, **kw):
        name = kw.get('name')
        sid = kw.get('studentID')
        if not(name and sid):
            raise UserError('学生姓名和学号不能为空')
            
        stu = http.request.env[ST.student].create({
            'name' : name, 'studentID' : sid
        })
        print('添加人员', stu, type(stu))
        return self._ansToClient( '添加成功', stu.name + ':' + stu.studentID )


        
    #删除
    @http.route('/student/del/', auth='public', website=True)
    def delOne( self, **kw):
        id = kw.get('id')
        if not id: raise UserError('无学生id')
        model = http.request.env[ST.student]    
        one = model.browse([ id ])
        if len(one) <= 0:
            raise UserError('没有这个学生信息')
                        
        one = one[0]
        one.unlink()
        return self._ansToClient('删除完成')

        
        
    # 更新信息
    @http.route('/student/update/', auth='public', website=True)
    def update( self, **kw ):
        id = kw.get('id')
        if not id: raise UserError('无学生id')
        
        name = kw.get('name')
        sid = kw.get('studentID')
        if not(name and sid):
            raise UserError('学生姓名和学号不能为空')
            
        model = http.request.env[ST.student]    
        one = model.browse([ id ])
        # print('查询结果-----------------------------', one)
        if len(one) <= 0:
            raise UserError('没有这个学生信息')
                        
        one = one[0]
        one.write({
            'name' : name, 'studentID' : sid
        })
        
        return self._ansToClient('更新成功', one.name )
        
        
    #搜索
    @http.route('/student/search/', auth='public', website=True)
    def search( self, **kw ):
        
        name = kw.get('name')
        if not name:
            raise UserError('学生姓名不能为空')
            
        return http.request.render('student.listing', {
            'title': '搜索结果',
            'root': '/student/student',
            'objects': http.request.env[ST.student].search([ ('name','like', name) ]),
        })
        
        
    # 统一返回数据，优化提示过慢    
    def _ansToClient( self, msg, infos = ''):
        # return http.request.render('student.message_page', {
        #     'msg': msg,
        #     'infos': infos
        # })
        datas = { 'success': 1, 'msg': msg, 'info': infos }
        jsonStr = json.dumps(datas, ensure_ascii=False )
        return jsonStr 